define(['echarts','select2'],function (echarts,select2) {
    return{
        init:function(){
          var $dateSelect = $('select[name="dateSelect"]');
          $dateSelect.select2({minimumResultsForSearch:-1});
          var number = GetQueryString('phoneNum');
          $dateSelect.on('change',function () {
              var value = $dateSelect.select2('val');
          });
          var lineStyle={normal:{color: '#38f',curveness: 0.3,width:4},symbolSize:[4,10]};
          $.ajax({
            url: "/callRecordsController/fingCallRecords",
            dataType: "json",
            type: "post",
            async: false,
            data: {numbering: number,dayNum:3},
            success:function (result) {
                var point=[{name: number, x: 550,y: 250,symbol : 'circle',symbolSize:85}],relation=[];
                if(result.code == '10001'){
                    var inhale = result.data.in,exhale = result.data.out;
                    for(var i=0;i<inhale.length;i++){
                        var distanceIn = parseInt(500/inhale.length),lineWidth=parseInt(inhale[i].value/3)>1?parseInt(inhale[i].value/3):1;
                        point.push({name:inhale[i].source,x:300,y:distanceIn*i+50,symbol:'rect'});
                        relation.push({source:inhale[i].source,target: number,number:inhale[i].value,lineStyle:{width:lineWidth}});
                    };
                    for(var j=0;j<exhale.length;j++){
                        var distanceOut = parseInt(500/exhale.length),lineWidth2=parseInt(exhale[j].value/3)>1?parseInt(exhale[j].value/3):1;
                        point.push({name:exhale[j].target,x:800,y:distanceOut*j+50,symbol:'rect'});
                        relation.push({source:number,target:exhale[j].target,number:exhale[j].value,lineStyle:{width:lineWidth2}});
                    };
                    showRelation(point,relation,number);
                }
            }
          });
        }
    };
    function showRelation(point,relation,number){
        var myChart = echarts.init(document.getElementById('relation'));
        option = {
            animationDurationUpdate: 1500,
            animationEasingUpdate: 'quinticInOut',
            series: [{
                type: 'graph',
                layout: 'none',
                symbolSize:[80,30],
                roam: true,
                edgeSymbol: ['circle', 'arrow'],
                edgeSymbolSize: [4, 6],
                legendHoverLink:true,
                draggable:true,
                legendHoverLink:true,
                edgeLabel: {// 连接线上的文字
                    normal: {
                        show: true,
                        textStyle: {
                            fontSize:12,
                        },
                        formatter:function (params) {
                            var value = params.data;
                            if(value.source == number){
                                return "呼出："+value.number+"次"
                            }else{
                                return "呼入："+value.number+"次"
                            }
                        },
                    },
                },
                focusNodeAdjacency:true,
                lineStyle: {
                    normal: {
                        opacity: 1,
                        width: 1,
                        curveness: 0.2
                    }
                },
                // 圆圈内的文字
                label: {
                    normal: {
                        show: true,
                        textStyle: {
                            fontSize:12,
                        },
                    },
                },
                data: point,
                links: relation
            }]
        };
        myChart.setOption(option);
        window.onresize = myChart.resize;
    };
    function GetQueryString (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    };
})